//
//  NSObject+TurnDictionary.m
//  MyDemo2
//
//  Created by ZSMAC on 2018/10/8.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "NSObject+TurnDictionary.h"

@implementation NSObject (TurnDictionary)
- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([self class], &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:cName];
        NSObject *value = [self valueForKey:name];
        NSMutableDictionary *keyName = [NSMutableDictionary dictionaryWithDictionary:@{@"ID":@"id",@"Namespace":@"namespace",@"descriptions":@"description"}];        name = keyName[name] != nil ? keyName[name] : name;
        if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
            [dic setObject:value forKey:name];
            
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
            [dic setObject:[self arrayOrDicWithObject:(NSArray*)value] forKey:name];
        } else if (value == nil) {
            
        } else {
            [dic setObject:[value dictionaryRepresentation] forKey:name];
            
        }
        
    }    return [dic copy];
    
}

- (id)arrayOrDicWithObject:(id)origin {
    if ([origin isKindOfClass:[NSArray class]]) {
        NSMutableArray *array = [NSMutableArray array];
        for (NSObject *object in origin) {
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                [array addObject:object];
                
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                [array addObject:[self arrayOrDicWithObject:(NSArray *)object]];
                
            } else {
                [array addObject:[object dictionaryRepresentation]];
            }
        }
        return [array copy];
    }else if ([origin isKindOfClass:[NSDictionary class]]) {
        NSDictionary *originDic = (NSDictionary *)origin;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (NSString *key in originDic.allKeys) {
            id object = [originDic objectForKey:key];
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                [dic setObject:object forKey:key];
                
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {                [dic setObject:[self arrayOrDicWithObject:object] forKey:key];
                
            } else {
                [dic setObject:[object dictionaryRepresentation] forKey:key];
                
            }
        }
        return [dic copy];
    }
    return [NSNull null];
    
}

@end
